package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.domain.WendaComment;
import cn.wolfcode.luowowo.comment.query.WendaCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IWendaCommentRepository;
import cn.wolfcode.luowowo.comment.service.IWendaCommentService;
import cn.wolfcode.luowowo.community.domain.Question;
import cn.wolfcode.luowowo.community.service.IQuestionService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class WendaCommentServiceImpl implements IWendaCommentService {



    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private IWendaCommentRepository dao;

    @Reference
    private IQuestionService questionService;


    public void save(WendaComment comment) {
        Question question = questionService.get(comment.getAnswerId());
       /* question.setReplynum(comment.getReplynum()+1);
        question.update(comment);*/
        comment.setCreateTime(new Date());
        String summary = comment.getContent();
        if(summary.length()>100){
            comment.setSummary(summary.substring(0,100));
        }else{
            comment.setSummary(summary);
        }
        dao.save(comment);
    }
    //查单个
    public WendaComment get(Long answerId){
        Query query = new Query();
        Criteria  criteria = Criteria.where("answerId").is(answerId);
        query.addCriteria(criteria);
        return mongoTemplate.findOne(query,WendaComment.class);
    }


    public Page query(WendaCommentQuery qo) {
            Query query = new Query();
            Criteria criteria = null;
            if(qo.getAnswerId()!=-1L){
                criteria = Criteria.where("answerId").is(qo.getAnswerId());
            }else{
                criteria = Criteria.where("answerId").ne(qo.getAnswerId());
            }
            query.addCriteria(criteria);

            long count = mongoTemplate.count(query, WendaComment.class);
            if (count == 0) {
                return Page.empty();
            }

            PageRequest of = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize());
            query.with(of);
            Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
            query.with(sort);

            List<WendaComment> wendaComments = mongoTemplate.find(query, WendaComment.class);
            return new PageImpl<WendaComment>(wendaComments, of, count);
        }

    public WendaComment commentAdd(String id, String content, UserInfo userInfo) {
        WendaComment wendaComment = new WendaComment();
        wendaComment.setCreateTime(new Date());
        wendaComment.setUsername(userInfo.getNickname());
        wendaComment.setHeadImgUrl(userInfo.getHeadImgUrl());
        wendaComment.setLevel(userInfo.getLevel());
        wendaComment.setUserId(userInfo.getId());
        wendaComment.setContent(content);
        WendaComment hd = dao.findById(id).get();
        hd.getComments().add(wendaComment);
        dao.save(wendaComment);
        hd.setReplynum(hd.getComments().size());
        dao.save(hd);
        return wendaComment;
    }

    public WendaComment replyComment(String hdId, String id, String content, UserInfo userInfo) {
        WendaComment wendaComment = new WendaComment();
        wendaComment.setCreateTime(new Date());
        wendaComment.setUsername(userInfo.getNickname());
        wendaComment.setHeadImgUrl(userInfo.getHeadImgUrl());
        wendaComment.setLevel(userInfo.getLevel());
        wendaComment.setUserId(userInfo.getId());
        wendaComment.setContent(content);
        //设置关联回复的评论
        WendaComment pl = dao.findById(id).get();
        wendaComment.setReplyComment(pl);
        //添加到回答的评论中
        WendaComment hd = dao.findById(hdId).get();
        hd.getComments().add(wendaComment);
        hd.setReplynum(hd.getComments().size());
        dao.save(wendaComment);
        dao.save(hd);
        return wendaComment;
    }

    public List<WendaComment> selectAll() {
        return dao.findAll();
    }

    public List<WendaComment> selectListByWendaId(Long id) {
        Query query = new Query();
        Criteria criteria = Criteria.where("answerId").is(id);
        query.addCriteria(criteria);
        return mongoTemplate.find(query,WendaComment.class);
    }

    @Override
    public List<WendaComment> selectForTop10() {
        Query query = new Query();
        query.with(PageRequest.of(0,5));
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<WendaComment> list = mongoTemplate.find(query, WendaComment.class);
        return list;
    }

    @Override
    public Long selectCountByQuestionId(Long questionId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("questionId").is(questionId));
        return mongoTemplate.count(query, WendaComment.class);
    }

}
